<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $bbsdata=M("data");
        $data=$bbsdata->order('id desc')->select();
        $bbscount=$bbsdata->count();
        $this->assign('bbscount',$bbscount);
        $this->assign('rows',$data);
        $this->display();
        
    }
    public function del()
     {
        $id=I('get.id',0);
        $bbsdata=M("data");
        $rs=$bbsdata->delete($id);
        if($rs)
        {
           $this->success('删除成功',U('Home/Index/index'));
        }
        else 
        {
            $this->error('删除失败');
        }
    }
    public function edit()
    {
        $id=I('get.id',0);
        $model=M("data");
        $r=$model->where("id=$id")->find();
        $this->assign("row",$r);
        $this->display();
    }
    
    public function update()
        {
            if(IS_POST)
                {
                  $id=I('get.id',0);
                  $data['username']=I('post.username','');
                  $data['sex']=I('post.sex','男');
                  $data['message']=I('post.message','');
                  $data['imgurl']=I('post.imgurl','');
                  $data['m_date']=date('Y-m-d H:i:s',  time());
                  $model=M("data");
                  $model->where("id=$id")->save($data);
                  $this->success('修改完成！',U('Home/Index/index'));
                } 
            else 
                {
                $this->error('非法操作！');
                }
        }
            
    public function add()
    {
        $model=D("data");// 实例化model对象
        if($model->create())
        {
            $model->m_date=date('Y-m-d H:i:s',  time());//为创建的数据对象新增日期
            $result = $model->add(); // 写入数据到数据库         
            if($result)
            {
                $this->success('留言成功！',U('Home/Index/index'));
            }
        else 
            {
            $this->error('留言失败');
            }
        } 
        else
            {
            $this->error($model->getError());//显示出错验证
        }
    }

}